Skip to content

feat(rust-examples): add mutual TLS example - #5995

Open
abidedavana wants to merge 2 commits into
aws:mainfrom
abidedavana:add-mutual-tls-example
Open

feat(rust-examples): add mutual TLS example#5995
abidedavana wants to merge 2 commits into
aws:mainfrom
abidedavana:add-mutual-tls-example

Conversation

@abidedavana

Copy link
Copy Markdown

Goal

Add a mutual TLS (client auth) example to the rust examples.

Why

Resolves #5584 — the examples cover basic TLS but none show mTLS, which is a
pretty common setup.

How

New mutual-tls member in bindings/rust-examples, based on the existing
tokio-server-client example. The server requires a client cert, trusts only
the example CA, and pins the client identity with a host name callback (the
docs note this is required, or the default rejects all client certs). The
client presents the existing wombat cert. README shows a good handshake plus
two rejections — no cert, and a valid-CA cert for the wrong identity.

Callouts

  • Reused the wombat cert as the client identity instead of adding a new one,
    since certs/generate.sh drops the CA key after signing and a new cert would
    mean regenerating every pem. Can add a dedicated client cert if you'd rather.
  • The server prints failed handshakes (the tokio example doesn't). Rejection is
    the point here, and it's otherwise invisible — with TLS1.3 the client still
    sees a successful handshake and the error only shows after the blinding delay.
    README explains both.

Testing

cargo build/test in bindings/rust-examples pass, fmt + clippy clean, and
I ran all three scenarios (the README outputs are real).

Related

resolves #5584

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Add a mutual TLS (client authentication) example to the rust examples,
modeled on the tokio-server-client example. The server requires client
certificates (ClientAuthType::Required), validates them against the
example CA only, and pins the expected client identity with a
VerifyHostNameCallback. The README demonstrates a successful mTLS
handshake plus two rejection cases: a client with no certificate and a
client with a trusted-CA certificate for the wrong identity.

// Split the stream.
// This allows us to call read and write from different tasks.
let (mut reader, mut writer) = tokio::io::split(tls);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally appreciate that you're just following the earlier example, but I honestly find the tokio::io::split to be kind confusing for the example. Could you instead

  1. client: send b"hello from the client"
  2. server: receive client message
  3. server: send b"good byte from the server"
  4. server: shutdown the write side
  5. client: receive the server message
  6. client: receive the shutdown
  7. client: shutdown the write side
  8. server: receive the client shutdown
  9. Finished!

This way it would also show the shutdown dance required to gracefully shutdown the TLS/TCP streams.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, that flow is much easier to follow, i swapped it in, Client sends its greeting, server replies with good byte from the server and closes its write side, client reads to EOF then closes its side, and the server does one last read to catch the client's shutdown. Updated the README outputs too.

Bonus: the rejected clients actually error out now instead of quietly exiting 0.

@abidedavana
abidedavana requested a review from jmayclin July 26, 2026 15:22
@kaukabrizvi
kaukabrizvi removed the request for review from jmayclin July 30, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Client auth to rust examples

2 participants